/*
 * @Author: 高富强
 * @Email: 875627570@qq.com
 * @Date: 2020-07-8 15:56:23
 * @Last Modified by: 高富强
 * @Last Modified time: 2020-07-9 09:56:23
 * @Description: 课程发布网络请教对象类，相当于BAL
 */
import 'dart:collection';
import 'dart:io';

import 'package:dio/dio.dart';

/// 课程发布相关
class ReqLessonPublish {
  final Dio _dio;
  static const TIME_OUT = 60000;
  static const CONTENT_TYPE_JSON = "application/json";
  static const CONTENT_TYPE_FORM = "application/x-www-form-urlencoded";
  ReqLessonPublish(this._dio);
  //课程发布
  Future<Response> publishLesson({
    String classIds, //班级id，逗号分隔
    String bookId, //教材id
    String bookType, //教材类型
    String moduleType, //模块类型：图文、视频、音频、小作业
    String content, //图文内容
    String menuIds, //章节id，逗号分隔
    String ownMenuIds, //自定义章节id，逗号分隔
    String bookMenuName, //章节名称，逗号分隔
    List<File> picFiles, //附件
    int audioTimeLength, //音频时长
    ProgressCallback progressCallback,
  }) async {
    var postFiles = [];
    //int i=0;
    await Future.forEach(picFiles, (item) async {
      //postFiles.add(await item);
      String path = item.path;
      var fileData = await MultipartFile.fromFile(path,
          filename: path.substring(path.lastIndexOf('/') + 1));
      postFiles.add(fileData);
      //i++;
    });
    Options options = new Options(method: "POST");

    ///超时
    options.receiveTimeout = TIME_OUT;
    Map<String, String> headers = new HashMap();
    //headers[Config.tokenName] = SPService.getToken();
    headers[HttpHeaders.contentTypeHeader] = CONTENT_TYPE_FORM;
    headers[HttpHeaders.acceptHeader] = CONTENT_TYPE_JSON;
    options.headers = headers;
//    print('附件：$postFiles'+postFiles[0].filename);
//    print('classIds：$classIds');
//    print('bookId：$bookId');
//    print('bookType：$bookType');
//    print('moduleType：$moduleType');
//    print('content：$content');
//    print('menuIds：$menuIds');
//    print('ownMenuIds：$ownMenuIds');
//    print('bookMenuName：$bookMenuName');
    Response response = await _dio.post('/prelesson/doPublish',
        data: FormData.fromMap({
          "files": postFiles,
          "classIds": classIds,
          "bookId": bookId,
          "bookType": bookType,
          "moduleType": moduleType,
          "content": content,
          "menuIds": menuIds,
          "ownMenuIds": ownMenuIds,
          "bookMenuName": bookMenuName,
          "audioTimeLength": audioTimeLength,
        }),
        options: options,
        onSendProgress: progressCallback);
    //print('发布返回数据：$response'); // 在需要生成model时需要json格式
    return response;
//官方表单网络请求示例:
//    FormData formData = FormData.fromMap({
//      "name": "wendux",
//      "age": 25,
//      "file": await MultipartFile.fromFile("./text.txt",filename: "upload.txt"),
//      "files": [
//        await MultipartFile.fromFile("./text1.txt", filename: "text1.txt"),
//        await MultipartFile.fromFile("./text2.txt", filename: "text2.txt"),
//      ]
//    });
//    response = await dio.post("/info", data: formData);
  }

  //课程二次发布
  Future<Response> rePublishLesson({
    String prelessonId, //备课id
    String classIds, //班级id，逗号分隔
    String bookId, //教材id
    String bookType, //教材类型
    String moduleType, //模块类型：图文、视频、音频、小作业
    String content, //图文内容
    String menuIds, //章节id，逗号分隔
    String ownMenuIds, //自定义章节id，逗号分隔
    String bookMenuName, //章节名称，逗号分隔
    List<File> picFiles, //附件
    int audioTimeLength, //音频时长
    ProgressCallback progressCallback,
    String oldImgsUrl, //已上传图片地址，逗号分隔
    String delModuleDetailIds, //小作业已删除图片的模块明细id，逗号分隔
  }) async {
//    print('prelessonId：'+prelessonId);
    print('附件：$picFiles');
//    print('classIds：$classIds');
//    print('bookId：$bookId');
//    print('bookType：$bookType');
//    print('moduleType：$moduleType');
//    print('content：$content');
//    print('menuIds：$menuIds');
//    print('ownMenuIds：$ownMenuIds');
//    print('bookMenuName：$bookMenuName');
    var postFiles = [];
    //int i=0;
    if (picFiles != null) {
      await Future.forEach(picFiles, (item) async {
        //postFiles.add(await item);
        String path = item.path;
        if (!path.contains('http')) {
          var fileData = await MultipartFile.fromFile(path,
              filename: path.substring(path.lastIndexOf('/') + 1));
          postFiles.add(fileData);
        }
      });
    }
    Options options = new Options(method: "POST");

    ///超时
    options.receiveTimeout = TIME_OUT;
    Map<String, String> headers = new HashMap();
    //headers[Config.tokenName] = SPService.getToken();
    headers[HttpHeaders.contentTypeHeader] = CONTENT_TYPE_FORM;
    headers[HttpHeaders.acceptHeader] = CONTENT_TYPE_JSON;
    options.headers = headers;

    return _dio.post(
      '/prelesson/doEditPublish',
      data: FormData.fromMap({
        "prelessonId": prelessonId,
        "files": postFiles,
        "classIds": classIds,
        "bookId": bookId,
        "bookType": bookType,
        "moduleType": moduleType,
        "content": content,
        "menuIds": menuIds,
        "ownMenuIds": ownMenuIds,
        "bookMenuName": bookMenuName,
        "audioTimeLength": audioTimeLength,
        "oldImgsUrl": oldImgsUrl,
        "delModuleDetailIds": delModuleDetailIds,
      }),
      options: options,
      onSendProgress: progressCallback,
    );
  }

  //课程二次发布加载信息
  Future<Response> publishInfo(
      {String prelessonId, ProgressCallback receiveProgressCallback}) async {
    return _dio.get(
      '/prelesson/publishInfo',
      queryParameters: {
        "prelessonId": prelessonId,
      },
      onReceiveProgress: receiveProgressCallback,
    );
  }
}
